package m4;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        //5672端口，客户端应用收发消息的端口
        //15672端口，管理控制台界面的访问端口
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();

        //创建direct类型交换机：direct_logs
//        c.exchangeDeclare("direct_logs", "direct");
        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        //向direct_logs交换机发送消息，并携带路由键关键词
        while (true) {
            System.out.println("请输入消息：");
            String s = new Scanner(System.in).nextLine();
            System.out.println("请输入路由键：");
            String k = new Scanner(System.in).nextLine();
            /*
                第二个参数：路由键
                    如果使用默认交换机（”“），默认使用队列名作为关键词，即路由键

             */
            c.basicPublish("direct_logs", k, null, s.getBytes());
        }

    }
}
